package com.wpml.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wpml.common.util.PageUtils;
import com.wpml.common.util.ResultJson;
import com.wpml.product.entity.BrandModel;
import com.wpml.product.entity.CategoryBrandRelationModel;
import com.wpml.product.service.CategoryBrandRelationService;
import com.wpml.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2019-11-17 21:25:25
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 获取当前品牌关联的所有分类列表
     */
    @GetMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public ResultJson cateloglist(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelationModel> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationModel>().eq("brand_id", brandId)
        );

        return ResultJson.ok(data);
    }

    /**
     * /product/categorybrandrelation/brands/list
     * <p>
     * 1、Controller：处理请求，接受和校验数据
     * 2、Service接受controller传来的数据，进行业务处理
     * 3、Controller接受Service处理完的数据，封装页面指定的vo
     */
    @GetMapping("/brands/list")
    public ResultJson relationBrandsList(@RequestParam(value = "catId") Long catId) {
        List<BrandModel> vos = categoryBrandRelationService.getBrandsByCatId(catId);

        List<BrandVo> collect = vos.stream().map(item -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());

            return brandVo;
        }).collect(Collectors.toList());

        return ResultJson.ok(collect);

    }


    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public ResultJson list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return ResultJson.ok(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public ResultJson info(@PathVariable("id") Long id) {
        CategoryBrandRelationModel categoryBrandRelation = categoryBrandRelationService.getById(id);

        return ResultJson.ok(categoryBrandRelation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public ResultJson save(@RequestBody CategoryBrandRelationModel categoryBrandRelation) {

        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return ResultJson.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public ResultJson update(@RequestBody CategoryBrandRelationModel categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return ResultJson.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public ResultJson delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return ResultJson.ok();
    }

}
